package com.zyb.system.shop.service.impl;

import com.zyb.constans.BaseConstants;
import com.zyb.system.employee.domain.Employee;
import com.zyb.system.employee.service.IEmployeeService;
import com.zyb.system.shop.domain.Shop;
import com.zyb.system.shop.domain.ShopAuditLog;
import com.zyb.system.shop.service.IShopAuditLogService;
import com.zyb.base.service.impl.BaseServiceImpl;
import com.zyb.system.shop.service.IShopService;
import org.apache.tomcat.util.bcel.classfile.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Arvin
 * @since 2023-01-09
 */
@Service
public class ShopAuditLogServiceImpl extends BaseServiceImpl<ShopAuditLog> implements IShopAuditLogService {
    @Autowired
    private IShopService shopService;

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private IEmployeeService employeeService;
    @Override
    public void add(ShopAuditLog shopAuditLog) {
        //设置保存时间
        shopAuditLog.setAuditTime(new Date());
        //获取审核人员的ID，以后再修改，添加假的数据
        shopAuditLog.setAuditId(333L);
        //获取到当前的商店
        Shop shop = shopService.queryById(shopAuditLog.getShopId());
        //修改状态
        shop.setState(shopAuditLog.getState());
        //保存数据
        shopService.update(shop);
        //获取管理员id
        Long adminId = shop.getAdminId();
        //或管理员的信息
        Employee admin = employeeService.queryById(adminId);
        String email = admin.getEmail();
        //发送邮件
        if(shopAuditLog.getState() == BaseConstants.Shop.STATE_WAIT_ACTIVE){
            //发送邮件
            sendEmail("宠物之家商铺激活",shop.getId(),email);
        }else {
            //发送邮件
            sendEmail("宠物之家商铺信息",null,email);
        }


        super.add(shopAuditLog);
    }

    private void sendEmail(String titleMsg,Long shopId,String toEmail){
        try {
            //创建复杂邮件对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            //发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("996305195@qq.com");
            helper.setSubject(titleMsg);
            //第二个参数 true 代表是否是一个html内容
            if(shopId == null){
                helper.setText("<h1>您注册的宠物之家的商铺已被驳回,如您需再次注册商铺请点击连接修改信息</h1>", true);
            }else {
                helper.setText("<h1>" + titleMsg + "</h1>" +
                "<a href='http://127.0.0.1:8080/shop/activation/" + shopId + "'>点击激活账号</a>", true);
            }
            //收件人
            helper.setTo(toEmail);
            //发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}
